/**
 * Created by wujiankun on 2016/12/1.
 */
const DOMAIN='https://art.163.com/artist';
// const DOMAIN='http://arttest.ws.netease.com';

const URL_LOGIN = '/v2/web/wechat/mini/user/query';
const URL_HOME = '/v2/web/zhanxun/home';
const URL_LIST = '/v2/web/zhanxun/list';
const URL_DETAIL = '/v2/web/zhanxun';
const URL_COMMENT_LIST = '/v2/web/zhanxun/comment/list';
const URL_SIGN = '/v2/web/zhanxun/signin';
const URL_ADD_COMMENT = '/v2/web/zhanxun/comment/add';
const URL_LIKE_COMMENT = '/v2/web/zhanxun/comment/like';
const URL_WANT_GO = '/v2/web/zhanxun/except';
const URL_INIT_DETAIL_STATE = '/v2/web/zhanxun/conditionStatus';
const URL_FILTER_TYPE_LIST = '/v2/web/dictionary/list?type=e_m_type';
function requestServer(url,data,callback) {
    if(!data.unionId){
        data.unionId =wx.getStorageSync('unionid');
    }
    wx.request({
        url: DOMAIN+url,
        method:"POST",
        data: data,
        header: {
            'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
        },
        success: res=>{
            callback(res.data);
        },
        fail:function (res) {
            console.log(res);
            if(res.data){
                callback(res.data);
            }else{
                wx.hideLoading();
                wx.showModal({
                    title: '网络异常',
                    content: '点击确定重新加载',
                    confirmText:'确定',
                    success: function(res) {
                        if (res.confirm) {
                            requestServer(url,data,callback);
                        }
                    }
                });
            }
        }
    })
}

function loginToServer(data,callback){
    requestServer(URL_LOGIN,data,callback)
}

function requestHome(callback){
    requestServer(URL_HOME,{},callback)
}

function requestList(data,callback){
    requestServer(URL_LIST,data,callback)
}
function requestDetail(data,callback){
    requestServer(URL_DETAIL+'/'+data.id,data,callback)
}
function wantToGO(data,callback){
    requestServer(URL_WANT_GO+'/'+data.id,data,callback)
}
function requestCommentList(data,callback){
    requestServer(URL_COMMENT_LIST,data,callback)
}

function commentAdd(data, callback) {
    requestServer(URL_ADD_COMMENT,data,callback);
}
function commentLike(data, callback) {
    requestServer(URL_LIKE_COMMENT+'/'+data.id,data,callback);
}

function doSign(id,callback){
    wx.getLocation({
        type: 'gcj02', //返回可以用于wx.openLocation的经纬度
        success (res) {
            const latitude = res.latitude;
            const longitude = res.longitude;
            requestServer(URL_SIGN+'/'+id,{
                longitude,latitude,id
            },callback)
        }
    })
}

function getInitDetailState(data,callback){
    requestServer(URL_INIT_DETAIL_STATE,data,callback);
}

function getFilterTypes(callback){
    requestServer(URL_FILTER_TYPE_LIST,{},callback);
}

module.exports = {
    requestServer,loginToServer,requestHome,requestList,requestDetail,requestCommentList,doSign,commentAdd,commentLike,
    wantToGO,getInitDetailState,getFilterTypes,URL_LIKE_COMMENT
};